package sctu.edu.java.year2016.team01.xuhanmei.day20160619;

import java.io.*;

/**
 * Created by Administrator on 2016/6/19.
 * 文件的拷贝
 */
public class CopyFile {
    public static void main(String[] args) throws IOException {
        String src="d:/java小程序/tree.png";
        String dest="d:/java小程序/01.jpg";
        copyFile(src,dest);
    }
    public static void copyFile(String srcpath,String destpath) throws IOException {

        //建立联系源，存在且为文件，目的地，，文件可以不存在
        File from=new File(srcpath);
        File to=new File(destpath);
        if(!from.isFile()){
            System.out.println("只能拷贝文件");
            throw  new IOException("只能拷贝文件");

        }

        //选择流

            InputStream is=new FileInputStream(from);
            OutputStream os=new FileOutputStream(to);

        //文件的拷贝，读取与写出
        byte[] bytes=new byte[1024];
        int len=0;

        while (-1!=(len=is.read(bytes))){
            os.write(bytes,0,len);
        }
        os.flush();//刷新

        //先开的后关
        os.close();
        is.close();
    }
}
